<template>
  <div class="app">
    <!-- 头部搜索栏 -->
    <cateHeader></cateHeader>

    <div class="p">
      <!-- 侧边栏 -->
      <div class="sidebar">
        <ul>
          <li v-for="(item, index) in menuItems" :key="index" :class="{ active: item.cat_id === selectedItemId }"
            @click="handleMenuClick(item.cat_id)">
            {{ item.cat_name }}
          </li>
        </ul>
      </div>
      <div class="cateSubject">
        <!-- 主要内容区域 -->
        <cateSubject :menuItems="menuItems" :selectedItemId="selectedItemId"></cateSubject>
      </div>
    </div>

    <!-- 底部导航栏 -->
    <div id="nav">
      <Nav></Nav>
    </div>
  </div>
</template>
  
<script>
//导航栏组件
import Nav from '../navbar.vue'
import cateHeader from '../../components/category/cateHeader.vue'
// import cateSidebar from '../../components/category/cateSidebar.vue'
import cateSubject from '../../components/category/cateSubject.vue'
import axios from 'axios';

export default {
  name: "category",
  // 注册组件
  components: {
    Nav,
    cateHeader,
    // cateSidebar,
    cateSubject,
  },
  data() {
    return {
      menuItems: [], // 将菜单项初始化为空数组
      selectedItemId: null // 存储选中的侧边栏 ID
    };
  },
  created() {
    this.getCategories();
 
  },
  methods: {
    getCategories() {
      axios.get('https://api-hmugo-web.itheima.net/api/public/v1/categories')
        .then(response => {
          this.menuItems = response.data.message;
          if (this.menuItems.length > 0) {
            this.selectedItemId = this.menuItems[0].cat_id; // 设置为第一个菜单项的ID
          }
        })
        .catch(error => {
          console.log(error);
        });
    },


    handleMenuClick(itemId) {
      this.selectedItemId = itemId;
      console.log(this.selectedItemId);// 更新选中的侧边栏 ID
    },
  },
}
</script>
  
<style scoped>
li.active {
  color: #ff0000;
  /* 将字体颜色设为红色 */
  border-left: 2px solid #ff0000;
  /* 添加左边红色竖条 */
}

.app {
  /* 添加滑块 */
  /* overflow-y: auto; */
}

.p {
  display: flex;
  max-height: 90vh;
  /* 设置最大高度，根据需要进行调整 */
  /* 添加滑块 */
  overflow-y: auto;
}

.cateSubject {
  width: 80%;
  max-height: 100vh;
  /* 设置最大高度，根据需要进行调整 */
  /* 添加滑块 */
  overflow-y: auto;
  margin-bottom: 67px;
}

.sidebar {
  width: 15%;
  min-height: 100vh;
  /* 设置最小高度为视口高度 */
  background-color: #eee6e6;
  padding: 20px;
  display: flex;
  justify-content: center;
  /* height: 100%; */
  max-height: 100vh;
  /* 设置最大高度，根据需要进行调整 */
  /* 添加滑块 */
  overflow-y: auto;
  /* 水平居中 */
  margin-bottom: 67px;
}

ul {
  list-style: none;
  padding: 0;
  margin: 0;
  width: 66px;
  /* 宽度为 .sidebar 的 95% */
  text-align: center;
  /* 将文本水平居中对齐 */
}

li {
  margin-bottom: 10px;
  padding-top: 11px;
  width: 66px;
  height: 23px;
  background-color: #e9ebeb;
  text-align: center;
  border-bottom: 1px solid #999;
  font-size: 14px;
}</style>